package com.cn.longdistancebusstation.personal;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.cn.longdistancebusstation.R;
import com.cn.longdistancebusstation.annotation.LoginStatus;
import com.cn.longdistancebusstation.base.BaseActivity;
import com.cn.longdistancebusstation.callback.OnCustomViewClickListener;
import com.cn.longdistancebusstation.global.GlobalVariable;
import com.cn.longdistancebusstation.httpHelper.HttpHelper;
import com.cn.longdistancebusstation.httpapi.HttpService;
import com.cn.longdistancebusstation.model.Result;
import com.google.gson.Gson;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by mucheng on 2018/1/25.
 */

public class AlterPasswordActivity extends BaseActivity implements View.OnClickListener{
    private EditText mCurrentPwdEditText,mNewPwdEditText,mSurePwdEditText;
    private Button btn_sure;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alter_password);
        getTitleTextView().setText("修改密码");
        initView();

    }

    private void initView() {
        mCurrentPwdEditText=(EditText)findViewById(R.id.current_pwd);
        mCurrentPwdEditText.setOnClickListener(this);
        mNewPwdEditText=(EditText)findViewById(R.id.new_pwd);
        mNewPwdEditText.setOnClickListener(this);
        mSurePwdEditText=(EditText)findViewById(R.id.sure_new_pwd);
        mSurePwdEditText.setOnClickListener(this);
        btn_sure=(Button)findViewById(R.id.sure_select_pwd);
        btn_sure.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.sure_select_pwd:
                sure();
                break;
        }

    }

    private void sure() {
        SharedPreferences sp=getSharedPreferences("data",MODE_PRIVATE);
        String current_username=sp.getString("telephone","");
        String current_pwd=mCurrentPwdEditText.getText().toString().trim();
        final String new_pwd=mNewPwdEditText.getText().toString().trim();
        String sure_new_pwd=mSurePwdEditText.getText().toString().trim();
        if(current_pwd.equals("")&&new_pwd.equals("")&&sure_new_pwd.equals("")) {
            Toast.makeText(AlterPasswordActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
        }else if(!new_pwd.equals(sure_new_pwd)){
            Toast.makeText(AlterPasswordActivity.this, "两次输入的密码不一样，请重新输入", Toast.LENGTH_SHORT).show();
        }else{
            showHud();
            Retrofit retrofit=new HttpHelper.HttpServiceBuilder().build().getRetrofit();
            HttpService httpService=retrofit.create(HttpService.class);
            Call<Result> call=httpService.editPassword(current_username,current_pwd,new_pwd);
            call.enqueue(new Callback<Result>() {
                @Override
                public void onResponse(Call<Result> call, Response<Result> response) {
                    hideHud();
                    Result result=response.body();
                    Boolean isSuccess=result.getHead().getSuccess();
                    if(isSuccess){
                        Toast.makeText(AlterPasswordActivity.this,"修改密码成功",Toast.LENGTH_SHORT).show();
                        SharedPreferences sharedPreferences = getSharedPreferences("data", MODE_PRIVATE);
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.putString("password", new_pwd);
                        editor.commit();
                        finish();
                    }else{
                        Toast.makeText(AlterPasswordActivity.this,"修改密码失败",Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onFailure(Call<Result> call, Throwable t) {
                    hideHud();
                    Toast.makeText(AlterPasswordActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

}
